<template>
<div>
  <div>
    <el-table
      :data="tableData.filter(data => !input || data.name.toLowerCase().includes(input.toLowerCase()))"
      style="width: 100%"
      max-height="550px"
      stripe="stripe"
      @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        type="index"
        width="50">
      </el-table-column>
      <el-table-column
        label="ID"
        prop="id"
        width="100">
      </el-table-column>
      <el-table-column
        label="Name"
        prop="name"
        width="100">
      </el-table-column>
      <el-table-column
        label="Password"
        prop="password">
      </el-table-column>
      <el-table-column
        align="right">
        <template slot="header" slot-scope="scope">
          <el-input
            v-model="input"
            size="mini"
            placeholder="输入关键字搜索"/>
        </template>
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)">Edit</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)">Delete</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <div style="position: relative; top: 8px">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="1"
      :page-sizes="[10, 20, 40,80 ]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination></div>
</div>
</template>

<script>
import api from '../../../assets/serviceAPI.config.js'
export default {
  name: 'UserList',
  data () {
    return {
      tableData: [],
      // 当前页
      page: 1,
      // 每页多少条
      pageSize: 10,
      // 总数
      total: 0,
      input: '',
      select: 'id',
      searchData: [],
      dialogTableVisible: false
    }
  },
  methods: {
    handleEdit (index, row) {
      console.log(index, row)
    },
    handleDelete (row) {
      this.$confirm('确认删除？' + row.name)
        .then(_ => {
          this.axios.get(api.deleteById, {
            params: {
              id: row.id
            }}).then((res) => {
            if (res.data.code === 0) {
              // this.getAll()
              // this.tableData.
            } else {
              //  服务器删除失败了
            }
          })
        })
        .catch(_ => {})
    },
    handleSizeChange (val) {
      this.pageSize = val
      this.getAll()
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange (val) {
      this.page = val
      this.getAll()
      console.log(`当前页: ${val}`)
    },
    getAll () {
      this.axios.get(api.selectAll, {
        params: {
          page: this.page,
          limit: this.pageSize
        }
      }).then((response) => {
        this.tableData = response.data.data
        this.total = response.data.count
      })
    },
    search (select, input) {
      if (input !== null && input !== '') {
        const params = new URLSearchParams()
        params.append(select, input)
        this.axios.post(api.getIfUsers, params).then((response) => {
          console.log(response.data.data)
          this.searchData = response.data.data
        })
      }
    },
    handleSelectionChange (row) {
      alert(row)
    }

  },
  mounted () {
    const that = this
    that.getAll()
  }
}
</script>
<style>
.select-with-input {
  width: 94px;
}
.input-with-select  {
  background-color: #fff;
  width: 300px;
}
</style>
